<template>
  <div class="content" v-title='"签到"'>
    <div class="top">
      <div class="sign">
        <div class="signImg">
          <div class="signState" :class="{grayBorder:flag}">
            <div class="noSign" v-show="!flag" @click="signAlertModel()">
              <p class="signText">签到</p>
              <p class="count">已累计
                <i>{{signDays}}</i>天</p>
            </div>
            <div class="signFinish" v-show="flag" @click="signAlertModel()">
              <p class="signText">已签到</p>
              <p class="count">已累计
                <i>{{signDays}}</i>天</p>
            </div>
          </div>
        </div>
      </div>
      <div class="integral" v-show="!flag">
        今日签到可获
        <i>{{signPoint}}</i>薏米
      </div>
      <div class="integral" v-show="flag">
        今日签到已获
        <i>{{signPoint}}</i>薏米
      </div>
      <div class="month">{{month}}月</div>
    </div>
    <div class="middle">
      <div class="arrow"></div>
      <ul class="week clearfix">
        <li v-for="week in weekTable">{{week}}</li>
      </ul>
      <ul class="date clearfix">
        <li v-for="day in dayArrays" :class="{spaceLeft:day.date==''}">
          <span v-show="!day.signed" :class="{dateColor:day.weekDay=='六' || day.weekDay=='日'}">{{day.date}}</span>
          <span v-show="day.signed">
            <span class="symbol"></span>
            <span class="dateNum">
              <i>{{day.date}}</i>
            </span>
            <span class="integralCount">+{{day.integral}}</span>
          </span>
        </li>
      </ul>
    </div>
    <div class="bottom">
      <div class="instruction">
        <span class="calendar"></span>
        <span>签到说明</span>
      </div>
      <div class="detail">
        <p>
          <span></span>连续签到可获取额外薏米奖励。
        </p>
        <p>
          <span></span>每日签到可获取2薏米，连续签到第三天可获得额外奖励7薏米，连续签到第7天可获得额外奖励15薏米。
        </p>
        <p>
          <span></span>签到周期为7天，每完成一个签到周期，签到重新计算。
        </p>
        <p>
          <span></span>若连续签到中断将从第一天开始重新计算。
        </p>
        <p>
          <span></span>如发现任何作弊行为，慧驿站将有权作废用户已获奖励。
        </p>
      </div>
    </div>
    <div v-if="showSignAlert" class="cover" @touchmove.stop.prevent>
      <sign-alert :class="{moduleBox:showSignAlert}" @showSignAlert="showAlert" :signPoint="signPoint" @goMall="goMall"></sign-alert>
    </div>
  </div>
</template>
<script>
import signAlert from './signAlert'
import { cookie } from '@/util/cookie'
import { getUrlParam, getOS } from '@/util/merge'
export default {
  data() {
    return {
      flag: false,
      signDays: 0, // 连续签到天数
      signPoint: 0, // 签到积分
      month: 0, // 月份
      showSignAlert: false, // 弹窗默认关闭
      weekTable: ['日', '一', '二', '三', '四', '五', '六'], // 星期
      dayArrays: [] // 签到信息列表
    }
  },
  components: {
    signAlert
  },
  methods: {
    signAlertModel() {
      // this.showSignAlert = true
      if (this.flag) {
        this.$dialog.notify({
          mes: '您今天已经签过到啦！',
          timeout: 3000
        })
        return
      }
      this.$post('/signModule/sign', {}).then((res) => {
        // 调用签到接口 (只有第一次签到res.resCode值为00000才执行，第二次点击签到res.resCode值为00003不执行)
        // console.log(res)
        this.showSignAlert = true
        this.signDays = res.record.signDays
      })
    },
    // 跳转到APP原生页
    goMall() {
      if (getOS() === 'ios') {
        /* eslint-disable no-undef */
        signinHandler()
      }
      if (getOS() === 'android') {
        window.Hstation.showIntegralMall()
      }
      this.showSignAlert = false
    },
    // 关闭弹窗
    showAlert() {
      this.getSign() // 调用签到历史接口重新获取签到日期和签到天数
      this.showSignAlert = false // 关闭弹窗
    },
    getSign() {
      // cookie('huiToken', 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDEwMDc3IiwiZXhwIjoxNTE1NjY1NTMxLCJpYXQiOjE1MTU2NTk1MzF9.vGIGV8UH2fi4ozUGS3454aycWEVVSojMtixX6pexuJ0')
      // this.initDayArrays('2018-01', [])
      this.$post('/signModule/signHistory', {}).then((res) => {
        // console.log(res)
        if (res.record.isSign === 'Y') {
          this.flag = true; // 判断是否签到的标志 ，'Y'表示已签到
        } else {
          this.flag = false; // 判断是否签到的标志，'N'表示未签到
        }
        this.signPoint = res.record.signPoint // 积分值
        this.signDays = res.record.signDays // 签到天数
        this.initDayArrays(res.record.nowDate, res.records) // 初始化日历
      })
    },
    initDayArrays(time, value) {
      var dates = []; // 存储已签到日期,积分
      var year = time.split('-')[0] // 取年份
      this.month = time.split('-')[1] // 取月份
      // console.log(year, this.month)
      value.forEach(function (v, i) {
        var date = { 'signDate': parseInt(v.signDate.split('-')[2]), 'signDesc': v.signDesc, 'signPoint': v.signPoint }
        dates.push(date);
      })

      // var date = new Date(2017, 10, 1);
      // this.month = date.getMonth();
      // var date = new Date();
      // this.month = date.getMonth() + 1;
      // var year = date.getFullYear()
      // var getdate = date.getDate();
      // console.log(getdate)

      // 获取月份天数
      function getMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
      }

      // 获取星期几
      function getWeekday(year, month, day) {
        return new Date(year, month, day).getDay();
      }
      this.weekTable = ['日', '一', '二', '三', '四', '五', '六']
      var totalDay = getMonthDays(year, this.month) // 当前月份天数
      var day = getWeekday(year, this.month - 1, 1) // 1号星期几
      this.dayArrays = [] // 签到信息列表
      for (var k = 0; k < day; k++) {
        this.dayArrays.push({ signed: false, date: '', integral: '', weekDay: '' }); // 1号之前this.dayArrays.date为空
      }

      for (var i = 1; i <= totalDay; i++) {
        var mark = false // 标记当月是否有签到
        var weekDayFlag = (day + i - 1) % 7 // 计算出当前日期对应的星期几
        if (dates.length !== 0) { // 当月签过到
          for (var j = 0; j < dates.length; j++) {
            if (dates[j]['signDate'] === i) { // 表示已签到
              mark = true;
              this.dayArrays.push({ signed: true, date: i, integral: dates[j]['signPoint'], weekDay: this.weekTable[weekDayFlag] })
              break // 结束本次循环
            }
            if (j === dates.length - 1 && !mark) { // 当前日期没有签到
              this.dayArrays.push({ signed: false, date: i, integral: '', weekDay: this.weekTable[weekDayFlag] })
              break // 结束本次循环
            }
          }
        } else { // 当月没有签过到
          this.dayArrays.push({ signed: false, date: i, integral: '', weekDay: this.weekTable[weekDayFlag] })
        }
      }
      // console.log(this.dayArrays)
      // this.dayArrays[6 + day] = { signed: true, date: 6, integral: '2', weekDay: '六' }
      // this.dayArrays[7 + day] = { signed: true, date: 7, integral: '3', weekDay: '日' }
      // this.dayArrays[25 + day] = { signed: true, date: 25, integral: '3', weekDay: '四' }
      // this.dayArrays[28 + day] = { signed: true, date: 28, integral: '3', weekDay: '日' }
      // this.dayArrays[31 + day] = { signed: true, date: 31, integral: '3', weekDay: '三' }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.token = getUrlParam('token')
      cookie('huiToken', this.token)
      this.getSign()
    })
  }
}
</script>
<style>
.content {
  width: 100%;
  height: 100%;
  background-color: #fff;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

.content .moduleBox {
  background-color: #5a9cf8;
}

.content .top {
  height: 5.89rem;
  background-image: url("../../assets/img/xhdp.png");
  background-repeat: no-repeat;
  background-size: cover;
  padding-top: 0.01rem;
  position: relative;
}

.top .sign {
  width: 3.27rem;
  height: 3.27rem;
  margin: 1.2rem auto 0;
}

.signImg {
  width: 3.09rem;
  height: 3.08rem;
  margin: 0.13rem auto;
  background-image: url("../../assets/img/xhdpOval.png");
  background-repeat: no-repeat;
  background-size: cover;
  padding-top: 0.01rem;
  /* -webkit-tap-highlight-color: rgba(0, 0, 0, 0); */
}

.signState {
  width: 2.58rem;
  height: 2.58rem;
  border-radius: 1.29rem;
  background-color: #fff;
  margin: 0.22rem auto;
  padding-top: 0.01rem;
}

.grayBorder {
  background-color: #ccd;
}

.noSign,
.signFinish {
  width: 100%;
  height: 100%;
  position: relative;
  color: #666;
  font-size: 0.46rem;
}

.noSign p,
.signFinish p {
  text-align: center;
}

.noSign p.signText,
.signFinish p.signText {
  margin-top: 0.88rem;
}

.noSign p.signText {
  color: #7ce4ff;
}

.count {
  font-size: 0.24rem;
  margin-top: 0.3rem;
}

.signState i {
  font-style: normal;
  margin-left: -0.06rem;
  vertical-align: top;
}

.integral {
  width: 2.66rem;
  height: 0.52rem;
  margin: 0 auto;
  text-align: center;
  color: #fff;
  font-size: 0.24rem;
  line-height: 0.52rem;
  border-radius: 1rem;
  background-color: #000;
  opacity: 0.5;
}

.integral i {
  font-style: normal;
  color: yellow;
  display: inline-block;
  margin-left: -0.05rem;
  margin-right: 0.01rem;
  vertical-align: top;
}

.month {
  position: absolute;
  left: 0.3rem;
  bottom: 0.44rem;
  width: 0.91rem;
  height: 0.41rem;
  background-image: url("../../assets/img/xhdpdate.png");
  background-repeat: no-repeat;
  background-size: cover;
  color: #fff;
  text-align: center;
  line-height: 0.48rem;
  font-size: 0.26rem;
}

.content .middle {
  width: 100%;
  position: relative;
}

.middle .arrow {
  position: absolute;
  left: 0.44rem;
  top: -0.32rem;
  width: 0;
  height: 0;
  border-width: 0.18rem;
  /*代表虚线的一个点*/
  border-style: dashed;
  border-color: transparent;
  border-bottom-style: solid;
  border-bottom-color: #fff;
}

.middle .week {
  list-style: none;
  height: 0.48rem;
  padding: 0.06rem 0.66rem;
  font-size: 0.26rem;
  line-height: 0.48rem;
  border-radius: 1px;
}

.middle .week li {
  float: left;
  margin-right: 0.72rem;
  width: 0.26rem;
  height: 0.37rem;
  line-height: 0.37rem;
}

.week li:last-child {
  margin-right: 0;
}

.week li:first-child,
.week li:last-child {
  color: #4c8ffb;
}

.date {
  margin: 0.1rem 0.3rem 0;
  list-style: none;
}

.date li {
  float: left;
  margin-right: 0.075rem;
  width: 0.91rem;
  height: 0.911rem;
  background-color: #ffffff;
  box-shadow: 0 0.02rem 0.15rem 0 rgba(190, 190, 190, 0.5);
  border-radius: 0.12rem;
  text-align: center;
  line-height: 0.911rem;
  font-size: 0.36rem;
  color: #333;
  margin-bottom: 0.116rem;
  position: relative;
}

.date li.spaceLeft {
  box-shadow: none;
  border-radius: 0;
}

.date li .dateColor {
  color: #999999;
}

.date li .symbol {
  width: 0.47rem;
  height: 0.47rem;
  position: absolute;
  left: 0.23rem;
  top: 0.15rem;
  background-image: url("../../assets/img/xhdpGroup.png");
  background-repeat: no-repeat;
  background-size: cover;
  z-index: 100;
}

.date li .dateNum {
  font-size: 0.13rem;
  color: #fff;
  width: 0.31rem;
  height: 0.38rem;
  position: absolute;
  left: 0;
  top: 0;
  background-image: url("../../assets/img/xhdpPath.png");
  background-repeat: no-repeat;
  background-size: cover;
  text-align: center;
}

.date .dateNum i {
  font-style: normal;
  position: absolute;
  width: 0.31rem;
  height: 0.38rem;
  left: -0.04rem;
  top: -0.33rem;
}

.date li .integralCount {
  position: absolute;
  width: 0.319rem;
  height: 0.28rem;
  left: 0.3rem;
  bottom: 0.3rem;
  font-size: 0.2rem;
  color: #5a9cf8;
}

.date li:last-child {
  margin-right: 0;
}

.content .bottom {
  width: 100%;
  margin-top: 0.1rem;
}

.bottom .instruction {
  font-size: 0.28rem;
  color: #333333;
  text-align: center;
  margin-bottom: 0.08rem;
}

.instruction span {
  display: inline-block;
  vertical-align: middle;
}

.instruction .calendar {
  width: 0.37rem;
  height: 0.39rem;
  margin-right: 0.204rem;
  background-image: url("../../assets/img/calendar.png");
  background-repeat: no-repeat;
  background-size: cover;
}

.bottom .detail {
  padding: 0 0.3rem 0.6rem;
  background-color: #fff;
}

.detail p {
  padding-left: 0.26rem;
  position: relative;
  font-size: 0.24rem;
  color: #666;
  /* margin-bottom: 0.08rem; */
  line-height: 0.4rem;
}

.detail p span {
  position: absolute;
  width: 0.18rem;
  height: 0.18rem;
  left: 0;
  top: 0.06rem;
  background-image: url("../../assets/img/circle.png");
  background-repeat: no-repeat;
  background-size: cover;
}
</style>


